﻿/* 
 * Copyright (c) 2012, CalixHu calixhu@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Clever Age nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DIaLOGIKa.b2xtranslator.StructuredStorage.Reader;
using DIaLOGIKa.b2xtranslator.DocFileFormat;
using DocumentFormat.OpenXml.Packaging;
using DIaLOGIKa.b2xtranslator.OpenXmlLib;
using DIaLOGIKa.b2xtranslator.WordprocessingMLMapping;
using DIaLOGIKa.b2xtranslator.OpenXmlLib.WordprocessingML;
using DocumentFormat.OpenXml.InkML;
using DIaLOGIKa.b2xtranslator.Tools;
using System.IO;
using DIaLOGIKa.b2xtranslator.StructuredStorage.Common;
using DIaLOGIKa.b2xtranslator.ZipUtils;
using System.Globalization;

namespace XSSRS.WordRenderer
{
    /// <summary>
    /// Summary description for Doc2X
    /// </summary>
    public class Doc2X
    {
        public static byte[] Convert(byte[] InputBytes, String ChoosenOutputFile)
        {
            byte[] ret = null;
            
            // convert
            if (ChoosenOutputFile == null)
            {
                ChoosenOutputFile = FileHelper.CreateTempFileName() + ".docx";
            }

            try 
            {
                //open the reader
                using (MemoryStream stream = new MemoryStream(InputBytes))
                {
                    using (StructuredStorageReader reader = new StructuredStorageReader(stream))
                    {
                        ret = ConvertInternal(reader, ChoosenOutputFile);                    }
                }
            }
            finally
            {
                FileHelper.DeleteTempFile(ChoosenOutputFile);
            }

            return ret;
        }

        public static byte[] Convert(byte[] InputBytes)
        {
            byte[] ret = null;

            //open the reader
            using (MemoryStream stream = new MemoryStream(InputBytes))
            {
                using (StructuredStorageReader reader = new StructuredStorageReader(stream))
                {
                    ret = ConvertInternal(reader, null);
                }
            }

            return ret;
        }

        protected static byte[] ConvertInternal(StructuredStorageReader reader, String ChoosenOutputFile)
        {
            //parse the input document
            WordDocument doc = new WordDocument(reader);
            byte[] bRet = null;
            //prepare the output document
            DIaLOGIKa.b2xtranslator.OpenXmlLib.OpenXmlPackage.DocumentType outType = Converter.DetectOutputType(doc);

            if (ChoosenOutputFile == null)
            {
                using (MemoryStream m2 = new MemoryStream())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        DIaLOGIKa.b2xtranslator.OpenXmlLib.WordprocessingML.WordprocessingDocument docx =
                        DIaLOGIKa.b2xtranslator.OpenXmlLib.WordprocessingML.WordprocessingDocument.Create(ms, outType);

                        //convert the document
                        Converter.Convert(doc, docx);
                        ms.WriteTo(m2);
                    }
                    bRet = m2.GetBuffer();
                }
            }
            else
            {
                string conformOutputFile = Converter.GetConformFilename(ChoosenOutputFile, outType);
                DIaLOGIKa.b2xtranslator.OpenXmlLib.WordprocessingML.WordprocessingDocument docx =
                  DIaLOGIKa.b2xtranslator.OpenXmlLib.WordprocessingML.WordprocessingDocument.Create(conformOutputFile, outType);
                Converter.Convert(doc, docx);
                bRet = FileHelper.FileToByteArray(ChoosenOutputFile);
            }
            return bRet;
        }
    }
}